package com.antexpress.service;

import com.antexpress.common.Response;
import com.antexpress.dao.AreaDAO;
import com.antexpress.dao.ExpressDistributionDAO;
import com.antexpress.pojo.MapCity;
import com.antexpress.pojo.MapDistrict;
import com.antexpress.pojo.MapProvince;
import com.antexpress.pojo.MapTown;
import com.antexpress.util.ExcelUtil;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.fileupload.FileItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AreaServiceImp implements AreaService{
    @Autowired
    private AreaDAO areaDAO;
    @Autowired
    private ExpressDistributionDAO expDao;
    
    @Override
    public Response excelUpload(List<FileItem> items, String realPath){
        for(FileItem item : items) {
            String filePath = null;
            String fileName = item.getName().toLowerCase();
            filePath = realPath+ fileName;
            if(filePath.matches("^.+\\.(?i)((xls)|(xlsx)|(xlsm)|(xltx)|(xltm)|(xlsb)|(xlam))$")){
                try {
                    File file = new File(filePath);
                    item.write(file);
                }catch (Exception ex) {
                    System.out.println("write file error!");
                    return Response.createByErrorMessage("Upload failed");
                }
                ExcelUtil util = new ExcelUtil(realPath,fileName,areaDAO,expDao,new File(filePath));
                try {
                    util.readXlsx(filePath, fileName, new File(filePath), areaDAO, expDao);
//                Thread excelUtil = new Thread(new ExcelUtil(realPath,fileName,areaDAO,expDao,new File(filePath)));
//                excelUtil.start();
                } catch (IOException ex) {
                    System.out.println("can't read file");
                }
            }
            else{
                return Response.createByErrorMessage("The error file");
            }
        }
        return Response.createBySuccessMessage("Upload successful.");
    }

    @Override
    public Response getAllProvinces() {
        Response resp = null;
        List<MapProvince> proList = areaDAO.getAllProvince();
        resp = Response.createBySuccess(proList);
        return resp;
    }

    @Override
    public Response getCityByProvinceId(MapProvince province) {
        Response resp = null;
        List<MapCity> cityList = areaDAO.getCityByProvice(province);
        resp = Response.createBySuccess(cityList);
        return resp;
    }

    @Override
    public Response getDistrictByCityId(MapCity city) {
        Response resp = null;
        List<MapDistrict> disList = areaDAO.getDistrictByCity(city);
        resp = Response.createBySuccess(disList);
        return resp;
    }

    @Override
    public Response getTownByDistrictId(MapDistrict district) {
        Response resp = null;
        List<MapTown> townList = areaDAO.getTownByDistrict(district);
        resp = Response.createBySuccess(townList);
        return resp;
    }

    @Override
    public Response getProvinceById(String id) {
        Response resp = null;
        MapProvince province = areaDAO.getProvinceById(id);
        resp = Response.createBySuccess(province);
        return resp;
    }

    @Override
    public Response getCityById(String id) {
        Response resp = null;
        MapCity city = areaDAO.getCityById(id);
        resp = Response.createBySuccess(city);
        return resp;
    }

    @Override
    public Response getDistrictById(String id) {
        Response resp = null;
        MapDistrict district = areaDAO.getDistrictById(id);
        resp = Response.createBySuccess(district);
        return resp;
    }

    @Override
    public Response getTownById(String id) {
        Response resp = null;
        MapTown town = areaDAO.getTownById(id);
        resp = Response.createBySuccess(town);
        return resp;
    }

    

}
